﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace PADWH35
{
    public partial class SetSafetyProductionBoard : Form
    {
        public SetSafetyProductionBoard()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox2.ReadOnly = true;

            }
            else
            {
                textBox2.ReadOnly = false;
               
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string dt = dateTimePicker1.Value.ToShortDateString();
                string dept = textBox1.Text;
                string temperature = textBox2.Text;
                string ifcomfromweb = checkBox1.Checked.ToString();
                string city = comboBox1.Text;
                string userid = textBox3.Text;
                
                string sql1 = $"select * from dbo.BoardInfo WHERE id='{userid}'";
                var result =ExecuteDt(sql1);
                if (result.Rows.Count>0)
                {
                    SaveConfig(userid, "userid");
                    SaveConfig(dt, "startTime");
                    SaveConfig(dept, "dept");
                    SaveConfig(temperature, "temperature");
                    SaveConfig(city, "city");
                    SaveConfig(ifcomfromweb, "ifcomfromweb");
                    MessageBox.Show("保存成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    string sql = $"UPDATE dbo.BoardInfo SET nvarchar01=N'{dt}',nvarchar02=N'{dept}',nvarchar03=N'{city}' WHERE id='{userid}'";
                    ExecuteSql(sql);
                }
                else
                {
                    MessageBox.Show("保存失败！请联系管理员！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch(Exception ee)
            {
                MessageBox.Show("保存失败！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private static string conStr = "Data Source=192.168.100.250;Initial Catalog=EpicorRelationData;User ID=szmis;pwd=szmis";
        public static int ExecuteSql(String Sqlstr, SqlParameter[] param = null, int Timeout = 30)
        {
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandTimeout = Timeout;
                cmd.Connection = conn;
                cmd.CommandText = Sqlstr;
                if (param != null) cmd.Parameters.AddRange(param);
                conn.Open();
                int i = cmd.ExecuteNonQuery();
                conn.Close();
                return i;
            }
        }

        public static DataTable ExecuteDt(String Sqlstr, SqlParameter[] param = null, int Timeout = 30)
        {
            try
            {
                using (SqlConnection conn = new SqlConnection(conStr))
                {

                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandTimeout = Timeout;
                    cmd.CommandText = Sqlstr;
                    if (param != null) cmd.Parameters.AddRange(param);
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    conn.Open();
                    da.Fill(dt);
                    conn.Close();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                WriteLog.Writelog(ex.ToString());
                return null;
            }
        }
        public void SaveConfig(string value, string name)
        {
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径  
            string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//AppDomain.CurrentDomain.BaseDirectory.ToString() + ".exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素  
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性  
                XmlAttribute att = nodes[i].Attributes["name"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素  
                if (att.Value == name)
                {
                    //对目标元素中的第二个属性赋值  
                    att = nodes[i].Attributes["connectionString"];
                    att.Value = value;
                    break;
                }
            }
            //保存上面的修改  
            doc.Save(strFileName);
        }
        public static string GetConfig(string name)
        {
            string value = null;
            XmlDocument doc = new XmlDocument();
            //获得配置文件的全路径  
            string strFileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;//AppDomain.CurrentDomain.BaseDirectory.ToString() + ".exe.config";
            doc.Load(strFileName);
            //找出名称为“add”的所有元素  
            XmlNodeList nodes = doc.GetElementsByTagName("add");
            for (int i = 0; i < nodes.Count; i++)
            {
                //获得将当前元素的key属性  
                XmlAttribute att = nodes[i].Attributes["name"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素  
                if (att.Value == name)
                {
                    //对目标元素中的第二个属性赋值  
                    att = nodes[i].Attributes["connectionString"];
                    value = att.Value;
                    break;
                }
            }
            //保存上面的修改  
            return value;
        }

        private void SetSafetyProductionBoard_Load(object sender, EventArgs e)
        {
            string dt = GetConfig("startTime");
            string dept = GetConfig("dept");
            string temperature = GetConfig("temperature");
            string ifcomfromweb = GetConfig("ifcomfromweb");
            string city = GetConfig("city");
            string userid= GetConfig("userid");
            DateTime date;
            bool IFcomfromweb;
            try
            {
                 date = Convert.ToDateTime(dt);
                IFcomfromweb = Convert.ToBoolean(ifcomfromweb);
                dateTimePicker1.Value = date;
                textBox1.Text = dept;
                textBox3.Text = userid;
                textBox2.Text = temperature;
                checkBox1.Checked = IFcomfromweb;
                comboBox1.Text = city;
                if (checkBox1.Checked)
                {
                    textBox2.ReadOnly = true;
                    
                }
                else
                {
                    textBox2.ReadOnly = false;
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取失败，请修改配置文件！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);             
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
